package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.BrandDao;
import cn.gjculture.shopping.api.dao.CommonUpdateMapper;
import cn.gjculture.shopping.api.dao.GoodsDao;
import cn.gjculture.shopping.api.entity.Brand;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.BrandService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    GoodsDao goodsDao;

    @Autowired
    CommonUpdateMapper commonUpdateMapper;

    @Override
    public List<Brand> query() {
        return brandDao.query();
    }

    @Override
    public Brand queryById(String brandId) {
        return brandDao.queryById(brandId);
    }

    @Override
    public int save(Brand brand) {
        brand.setBrandId(UUIDUtil.getUUID());
        brand.setInsertTime(DateUtil.getTimestamp());
        brand.setUpdateTime(DateUtil.getTimestamp());
        return brandDao.save(brand);
    }

    @Override
    public int delete(String brandId, Return ret) {
        Integer goodsCount = goodsDao.queryByBrandId(brandId);
        if (goodsCount != null && goodsCount > 0) {
            ret.getCommon().setMessage("商品下面有商品，不能删除");
            return 0;
        }
        return brandDao.delete(brandId);
    }

    @Override
    public int update(Brand brand) {
        brand.setUpdateTime(DateUtil.getTimestamp());
        return commonUpdateMapper.updateBrand(brand);
    }
}
